<?php
namespace app\restaurant\controller;
use app\AdminRest;

use app\restaurant\model\OrderGoods;
use app\restaurant\model\Printer;
use app\restaurant\model\Progress;
use app\restaurant\model\Table;
use app\restaurant\model\TransporterOrder;
use app\restaurant\server\Context;
use app\shop\model\AdminCompany;
use longbingcore\wxcore\DadaApi;
use think\App;

use app\restaurant\model\Order as model;

use app\restaurant\model\OrderAddress;

use app\restaurant\model\Store;

use app\restaurant\model\RefundOrder;


use think\facade\Db;


class AdminOrder extends AdminRest
{

    protected $model;

    protected $company;

    protected $address_model;

    protected $store_model;

    protected $refund_model;

    protected $progress_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model   = new Model();

        $this->company = new AdminCompany();

        $this->address_model = new OrderAddress();

        $this->store_model  = new Store();

        $this->refund_model = new RefundOrder();

        $this->progress_model = new Progress();



    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:列表
     */
    public function orderList(){

        //取消过期订单
        $this->model->checkOrder($this->_uniacid);
        //自动完成
        $this->model->checkEndOrder($this->_uniacid);
        //审核过期售后订单
        $this->refund_model->checkOrder($this->_uniacid,$this->payConfig($this->_uniacid));

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //时间查询
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }
        //订单号查询
        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }
        //手机号
        if(!empty($input['mobile'])){

            $dis[] = ['e.phone','like','%'.$input['mobile'].'%'];
        }
        if(!empty($input['name'])){

            $dis[] = ['e.name','like','%'.$input['name'].'%'];
        }
        //门店查询
        if(!empty($input['store_title'])){

            $dis[] = ['b.title','like','%'.$input['store_title'].'%'];
        }
        //状态
        if(isset($input['pay_type'])&&$input['pay_type']!=999){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        //类型
        if(isset($input['order_type'])&&$input['order_type']!=999){

            $dis[] = ['a.order_type','=',$input['order_type']];

        }

        //公司
//        if(!empty($this->_input['company'])){
//
//            $company = $input['company'];
//
//            $cpy_ids = $this->company->companyLow($company);
//
//            array_unshift($cpy_ids,$company);
//
//            $dis[] = ['a.company_id','in',$cpy_ids];
//        }
        //商品
        if(!empty($input['goods_name'])){

            $dis[] = ['f.goods_name','like','%'.$input['goods_name'].'%'];
        }

        $data = $this->model->adminOrderList($dis,$input['limit']);


        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-26 15:57
     * @功能说明:订单详情1
     */
    public function orderInfo(){

        $input = $this->_param;

        $order = $this->model->orderInfo(['id'=>$input['id']]);

        $order['address']    = $this->address_model->dataInfo(['order_id'=>$order['id']]);

        $order['store_info'] = $this->store_model->storeInfo(['id'=>$order['store_id']]);

        $transporter_model = new TransporterOrder();
        //配送信息
        $transporter_info  = $transporter_model->orderInfo(['order_id'=>$order['id']],['status','transporter_id','transporter_name','transporter_phone','distance']);
        //配送进度
        $transporter_info['progress'] = $this->progress_model->dataList(['order_code'=>$order['order_code']]);

        $order['transporter_info']    = $transporter_info;

        if($order['order_type']==1){

            $order_goods_model = new OrderGoods();

            $order['order_goods'] = $order_goods_model->houseOrderGoods($order['id']);
        }
        //员工id
        $order['staff_name']     = lbUserName($order['staff_id']);

        $order['end_staff_name'] = lbUserName($order['end_staff']);
        //
        $table_model = new Table();
        //桌号id
        $order['table_name'] = $table_model->where(['id'=>$order['table_id']])->value('title');

        return $this->success($order);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-06 15:26
     * @功能说明:退款列表
     */
    public function refundOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.user_cancel','=',0];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        if(isset($input['order_type'])&&$input['order_type']!=999){

            $dis[] = ['b.order_type','=',$input['order_type']];
        }

        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['b.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['refund_order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['refund_order_code'].'%'];

        }

        if(!empty($input['store_title'])){

            $dis[] = ['e.title','like','%'.$input['store_title'].'%'];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $data = $this->refund_model->adminRefundOrderList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-05 13:42
     * @功能说明:退款订单详情
     */
    public function refundOrderInfo(){

        $input = $this->_input;

        $store_model       = new Store();

        $progress_model    = new Progress();

        $transporter_model = new TransporterOrder();

        $order = $this->refund_model->orderInfo(['id'=>$input['id']]);
        //用户信息
        $order['address']    = $this->address_model->dataInfo(['order_id'=>$order['order_id']]);
        //用户头像
        $order['user_img']   = lbUserAvatar($order['user_id']) ;
        //门店信息
        $order['store_info'] = $store_model->where(['id'=>$order['store_id']])->field('title,mobile')->find();
        //支付订单
        $pay_order = $this->model->orderInfo(['id'=>$order['order_id']]);
        //当前进度
        $order['now_progress'] = $progress_model->nowOrderTextRefund($pay_order['order_code']);
        //当前骑手状态
        $order['transporter_status'] = $transporter_model->where(['order_id'=>$order['order_id']])->value('status');

        $order['create_time'] = date('Y-m-d H:i:s',$order['create_time']);

        $order['sh_time']     = !empty($order['sh_time'])?date('Y-m-d H:i:s',$order['sh_time']):0;

        $order['pay_order_code'] = $pay_order['order_code'];

        $transporter_model = new TransporterOrder();
        //配送信息
        $transporter_info  = $transporter_model->orderInfo(['order_id'=>$order['order_id']],['status','transporter_id','transporter_name','transporter_phone','distance']);
        //配送进度
        $transporter_info['progress'] = $this->progress_model->dataList(['order_code'=>$pay_order['order_code']]);

        $order['transporter_info']  = $transporter_info;

        $order['order_type']    = $pay_order['order_type'];

        $order['pay_create_time'] = date('Y-m-d H:i:s',$pay_order['create_time']);

        return $this->success($order);

    }




    /**
     * @author chenniang
     * @DataTime: 2020-11-04 14:48
     * @功能说明:后台同意退款
     */
    public function agreeRefund(){

        $input = $this->_input;

        $refund_order = new RefundOrder();

        $res = $refund_order->agreeRefund($input['id'],$input['price'],$this->payConfig($this->_uniacid),1);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-04 14:48
     * @功能说明:后台拒绝退款
     */
    public function noAgreeRefund(){

        $input = $this->_input;

        $refund_order = new RefundOrder();

        $res = $refund_order->noAgreeRefund($input['id'],1);

        return $this->success($res);

    }












}
